package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.ExcelSubjectData;
import com.atguigu.eduservice.entity.subject.SubjectListOne;
import com.atguigu.eduservice.entity.subject.SubjectListTwo;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-12-24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20002,"添加课程分类失败");
        }

    }

    /**
     * 查询一级和二级分类
     * @return
     */
    @Override
    public List<SubjectListOne> getAllOneTwoSubject() {
        //查询出所有课程
        List<EduSubject> list = this.list();
        //过滤出一级课程
        List<EduSubject> collectOne = list.stream().filter(eduSubject -> "0".equals(eduSubject.getParentId())).collect(Collectors.toList());

        //创建List集合，用于存储最终封装数据
        List<SubjectListOne> finalList ;

        //strem() 是将list里面的数据变成流的形式，
        //然后将每个list中的数据传入到map中的方法中去,然后我们可以对list中的数据进行操作，
        //并通过collect(Collectors.toList())构建成新的list
        finalList=collectOne.stream().map((item) ->{

            SubjectListOne subjectListOne =new SubjectListOne();
            List<SubjectListTwo> list2 = new ArrayList<>();

            BeanUtils.copyProperties(item, subjectListOne);//先把获取到的一级目录拷贝给subjectListOne

            LambdaQueryWrapper<EduSubject> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(EduSubject::getParentId,item.getId());
            List<EduSubject> list1 = this.list(lambdaQueryWrapper);//条件查询出parentid等于一级课程id的二级课程
            for (EduSubject edu1:list1 //遍历出他们每一个
                 ) {
                SubjectListTwo subjectListTwo=new SubjectListTwo();
                BeanUtils.copyProperties(edu1,subjectListTwo);//先给SubjectListTwo
                list2.add(subjectListTwo);//再把SubjectListTwo给list
            }
            subjectListOne.setChildren(list2);//最后把list给children

            return subjectListOne;//我发现stream流必须返回中间new的对象
        }).collect(Collectors.toList());


        return finalList;
    }
}
